<?php
namespace Services;

use Helpers\Tool;
use Helpers\Main;
use Helpers\HttpClient;
use Helpers\XhException;

class CloudTask
{

    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;

    /**
     * 初始化配置
     *
     * @throws XhException
     *
     * create by ck 20180719
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config     = Main::getConfigByName('stats_CA103001-3');
            self::$url  = Tool::getValue($config, 'url');
            if (!self::$url) {
                throw new XhException('服务url获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 获取学校教师书本使用情况
     *
     * @param $schoolId
     * @param null $startTime
     * @param null $endTime
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by ck 20180719
     * modify by ck 20180929
     */
    public static function getTchBookUseInfo($schoolId, $startTime = null, $endTime = null)
    {
        self::init();
        $params = [];
        if ($startTime) {
            $params['startTime'] = $startTime;
        }
        if ($endTime) {
            $params['endTime'] = $endTime;
        }
        $response   = self::$httpClient->get(
            sprintf('/intranet/work/statistic/schools/%d/bookUseInfo', $schoolId),
            $params
        );
        if (!$response->isSuccess()) {
            throw new XhException('信息获取失败', $response);
        }
        $data = $response->toArray();
        $libraryType = ResBase::getDictionaries('libraryType');
        Tool::runEnum($data, ['libraryType' => $libraryType]);
        return $data;
    }

    /**
     * 获取学校教师书本使用详情
     *
     * @param $schoolId
     * @param $teacherId
     * @param null $startTime
     * @param null $endTime
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by ck 20180719
     * modify by ck 20180726
     */
    public static function getTchBookUseDetail($schoolId, $teacherId, $startTime = null, $endTime = null)
    {
        self::init();
        $params = [
            'size'      => 0,
//            'teacherId' => $teacherId,
            'lastTime'  => 0
        ];
        if ($startTime) {
            $params['startTime'] = $startTime;
        }
        if ($endTime) {
            $params['endTime'] = $endTime;
        }
        $response   = self::$httpClient->get(
            sprintf('/intranet/works/schools/%d/teacher/%s/', $schoolId, $teacherId),
            $params
        );
        if (!$response->isSuccess()) {
            throw new XhException('信息获取失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 获取书本使用情况
     *
     * @param $schoolId
     * @param $bookId
     * @param array $params
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by qgz 20180820
     * modify by ck 20180912
     */
//    public static function getBookUseDetail($params)
    public static function getBookUseDetail($schoolId, $bookId, $params = [])
    {
        self::init();
        $response = self::$httpClient->get(
            sprintf('/intranet/work/statistic/schools/%d/books/%s/', $schoolId, $bookId),
            $params
        );
        if (!$response->isSuccess()) {
            throw new XhException('信息获取失败', $response);
        }
        return $response->toArray();
    }

}